﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LumenWorks.Framework.IO.Csv;

namespace Visualization.DataModel
{
    public class ParticleReader
    {
        //List<SFPStep> SFPSteps = new List<SFPStep>();
        public Dictionary<int, SFPStep> SFPSteps = new Dictionary<int, SFPStep>();
        public void readCSV(string filename)
        {
            using (CsvReader reader = new CsvReader(new StreamReader(filename), false))
            {
                reader.ParseError += new EventHandler<ParseErrorEventArgs>(reader_ParseError);
                reader.ReadNextRecord();
                string[] test = reader.ElementAt(0);
                Particle ptcle =  new Particle();
                List<Particle> current = new List<Particle>();
                SFPStep currentStep = new SFPStep();
                bool skip = false;  //so that the program skips particles from a duplicate step
                foreach(string[] entry in reader){
                    if (entry[0] == "PF" && !skip)
                    {
                        try
                        {
                            ptcle = createParticleData(entry);
                            current.Add(ptcle);
                        }
                        catch (Exception err)
                        {
                            //tell user about corrupt data somehow
                        }
                        //particles
                    }
                    else if (entry[0] == "STEP")
                    {
                        currentStep.particles = current;
                        if ((int)Convert.ToDouble(entry[1]) != 0)
                        {
                            if (!SFPSteps.ContainsKey((int)Convert.ToDouble(entry[1])))
                            {
                                SFPSteps.Add((int)Convert.ToDouble(entry[1]), currentStep);
                                currentStep = new SFPStep((int)Convert.ToDouble(entry[1]), Convert.ToDouble(entry[2]));
                                current = new List<Particle>();
                                skip = false;
                            }
                            else
                                skip = true;
                        }
                    }
                    //new step sensor fusion process step
                    else if (entry[0] == "SFP" && !skip) {
                        currentStep.X = Convert.ToDouble(entry[1]);
                        currentStep.Y = Convert.ToDouble(entry[2]);
                        currentStep.Theta = Convert.ToDouble(entry[3]);
                    }
                    //sensor fustion process predicted values
                    else { }
                        //Don't need to do anything...maybe?
                }
            }
        }

        private Particle createParticleData(string[] entry)
        {
            Particle temp = new Particle();
            temp.X = Convert.ToDouble(entry[2]);
            temp.Y = Convert.ToDouble(entry[3]);
            temp.Theta = Convert.ToDouble(entry[4]);
            temp.NormalizationError = Convert.ToDouble(entry[5]);
            return temp;
        }

        void reader_ParseError(object sender, ParseErrorEventArgs e)
        {
//            System.Windows.MessageBox.Show("Uh oh, an error!\n" + e.Error);
        }
    }
}
